from flask import Blueprint, render_template, redirect, url_for
from flask_login import login_required, current_user
from app.models import Teacher, Student, Course, Score
from app import db

main = Blueprint('main', __name__)

@main.route('/')
def index():
    if current_user.is_authenticated:
        return redirect(url_for('dashboard.index'))
    return render_template('index.html')

@main.route('/profile')
@login_required
def profile():
    if isinstance(current_user, Teacher):
        user_type = 'teacher'
        students = Student.query.filter_by(teacher_id=current_user.id).all()
        return render_template('profile.html', user=current_user, user_type=user_type, students=students)
    else:
        user_type = 'student'
        # 获取该学生的所有课程成绩
        scores = Score.query.filter_by(student_id=current_user.id).all()
        course_scores = {}
        
        for score in scores:
            if score.course.name not in course_scores:
                course_scores[score.course.name] = []
            course_scores[score.course.name].append(score)
            
        return render_template('profile.html', user=current_user, user_type=user_type, course_scores=course_scores)
        
@main.route('/courses')
@login_required
def courses():
    all_courses = Course.query.all()
    return render_template('courses.html', courses=all_courses) 